package com.ruoyi.job.controller;

import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.job.service.BatchPaymentServiceImpl;
import com.ruoyi.job.service.ISysJobService;
import com.ruoyi.job.thread.TGThread;
import org.eclipse.paho.client.mqttv3.MqttClient;
import org.quartz.SchedulerException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Random;
import java.util.UUID;

/**
 * @Author: lek
 * @Description: TODO
 * @Date: 2025/3/10 18:24
 * @Version: 1.0
 */
@RestController
@RequestMapping("/lll")
public class LllController {
    @Autowired
    private ISysJobService jobService;
    private static final Random random = new Random();
    private MqttClient mqttClient;
    private TGThread publishThread;
    private TGThread subscribeThread;
    String brokerUrl = "tcp://124.223.12.211:1883";
    String clientId = "temperature_publisher" +  UUID.randomUUID().toString().substring(0, 8);
    String topic = "sensor/data";
    int pubQos = 1;

    /**
     * @author maisan
     * @description //TODO 运输完成时调用    关闭温湿度GPS多线程进程
     * @createTime  2025/3/14 17:32
     * @param [java.lang.String]
     * @return com.ruoyi.common.core.web.domain.AjaxResult
     **/
    @PostMapping("/closeMqtt")
    public void closeMqtt(@RequestParam("licensePlate") String licensePlate)
    {
        publishThread.getConsoleThreadMap().get("publish"+licensePlate).interrupt();
        subscribeThread.getConsoleThreadMap().get("subscribe"+licensePlate).interrupt();
        System.out.println("线程关闭");
    }

    /**
     * @author maisan
     * @description //TODO 开始运输时调用   启动mqtt并接收mqtt信息 改变温湿度GPS状态
     * @createTime  2025/3/14 17:32
     * @param
     * @return com.ruoyi.common.core.web.domain.AjaxResult
     **/

    @PostMapping("/startmqtt")
    public AjaxResult changeStatus(@RequestParam("licensePlate") String licensePlate) throws SchedulerException
    {
        System.out.println(licensePlate);
//        SysJob newJob = jobService.selectJobById(100L);
//        newJob.setStatus(status);
//        return AjaxResult.success(jobService.changeStatus(newJob));
        publishThread = new TGThread();
        publishThread.setSerial(licensePlate);
        publishThread.setCmd("publish");
        publishThread.start();
        subscribeThread = new TGThread();
        subscribeThread.setSerial(licensePlate);
        subscribeThread.setCmd("subscribe");
        subscribeThread.start();
        System.out.println("sub线程接收信息：" + subscribeThread.getSubMessage());

        return AjaxResult.success();
    }

    @Autowired
    private BatchPaymentServiceImpl batchPaymentService;
//    @GetMapping("/test")
//    public void test01() throws Exception{
//
//        batchPaymentService.t();
//
//        batchPaymentService.doTaskOne();
//        batchPaymentService.doTaskTwo();
//        batchPaymentService.doTaskThree();
//        System.out.println("执行完了");
//
//    }
}
